package yuejian_07.yuejian_07.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import yuejian_07.yuejian_07.R;
import yuejian_07.yuejian_07.activity.TrainInfoActivity;
import yuejian_07.yuejian_07.javabean.Train;


import static yuejian_07.yuejian_07.base.Constant.PHOTO_URL;



public class MasterClassAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;

    public MasterClassAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Train entity = (Train) mDataList.get(position);
        if (null == entity)
            return;
        //绑定数据
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.t_name.setText(entity.getTrain_name());
        System.out.printf("master_name");
        viewHolder.t_hot.setText(entity.getTrain_hot()+ "");
        String train_pic =PHOTO_URL+entity.getTrain_pic();

        Picasso.with(mContext).load(train_pic).into(viewHolder.t_pic);
        if(viewHolder.t_pic.getDrawable()==null){
            viewHolder.t_pic.setImageResource(R.drawable.no_pic);
        }
        //点击item跳转
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();
                bundle.putInt("train_id",entity.getTrain_id());
                //bundle.putInt("master_id",entity.getMaster_id());
                Intent intent=new Intent(mContext, TrainInfoActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });


    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建ViewHolder
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_masterclass, parent, false);
        return new ViewHolder(v);

    }

    //自定义的V
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView t_pic;
        TextView t_name,t_hot;

        public ViewHolder(View itemView) {
            super(itemView);
            t_name = (TextView) itemView.findViewById(R.id.name);
            t_pic = (ImageView) itemView.findViewById(R.id.pic);
            t_hot= (TextView) itemView.findViewById(R.id.hot);
        }
    }

}
